<?php
session_start();
require_once '../config.php';
require_once '../functions.php';

// 检查管理员是否登录
if (!is_admin_logged_in()) {
    header('Location: login.php');
    exit;
}

// 获取统计数据
try {
    // 表白总数
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM confessions");
    $confession_count = $stmt->fetch()['count'];
    
    // 日常总数
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM dailies");
    $daily_count = $stmt->fetch()['count'];
    
    // 公告总数
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM announcements");
    $announcement_count = $stmt->fetch()['count'];
    
    // 评论总数
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM comments");
    $comment_count = $stmt->fetch()['count'];
    
    // 违禁词总数
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM prohibited_words");
    $word_count = $stmt->fetch()['count'];
    
    // 封禁IP总数
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM banned_ips");
    $ban_count = $stmt->fetch()['count'];
    
    // 最近的表白
    $stmt = $pdo->query("SELECT * FROM confessions ORDER BY created_at DESC LIMIT 5");
    $recent_confessions = $stmt->fetchAll();
    
    // 最近的日常
    $stmt = $pdo->query("SELECT * FROM dailies ORDER BY created_at DESC LIMIT 5");
    $recent_dailies = $stmt->fetchAll();
} catch (PDOException $e) {
    $error = "获取数据失败: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理仪表盘 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/all.min.css">
    <link rel="stylesheet" href="../static/css/style.css">
</head>
<body>
    <div class="sidebar col-md-3 col-lg-2 d-md-block">
        <div class="sidebar-header">
            <h2><?php echo SITE_NAME; ?> 管理后台</h2>
        </div>
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link active" href="index.php">
                    <i class="fas fa-tachometer-alt"></i> 仪表盘
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="confessions.php">
                    <i class="fas fa-heart"></i> 表白管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="dailies.php">
                    <i class="fas fa-coffee"></i> 日常管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="announcements.php">
                    <i class="fas fa-bullhorn"></i> 公告管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="comments.php">
                    <i class="fas fa-comments"></i> 评论管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="prohibited_words.php">
                    <i class="fas fa-ban"></i> 违禁词管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="banned_ips.php">
                    <i class="fas fa-shield-alt"></i> 封禁管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="settings.php">
                    <i class="fas fa-cog"></i> 系统设置
                </a>
            </li>
        </ul>
        
        <div class="logout-btn">
            <a href="logout.php">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a>
        </div>
    </div>
    
    <main class="main-content">
        <h1 class="page-title"><i class="fas fa-tachometer-alt"></i> 管理仪表盘</h1>
        
        <?php if (isset($error)): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <div class="row">
            <div class="col-md-4 col-sm-6">
                <div class="stat-card">
                    <i class="fas fa-heart"></i>
                    <div class="stat-number"><?php echo $confession_count; ?></div>
                    <div class="stat-label">表白总数</div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="stat-card">
                    <i class="fas fa-coffee"></i>
                    <div class="stat-number"><?php echo $daily_count; ?></div>
                    <div class="stat-label">日常总数</div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="stat-card">
                    <i class="fas fa-bullhorn"></i>
                    <div class="stat-number"><?php echo $announcement_count; ?></div>
                    <div class="stat-label">公告总数</div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="stat-card">
                    <i class="fas fa-comments"></i>
                    <div class="stat-number"><?php echo $comment_count; ?></div>
                    <div class="stat-label">评论总数</div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="stat-card">
                    <i class="fas fa-ban"></i>
                    <div class="stat-number"><?php echo $word_count; ?></div>
                    <div class="stat-label">违禁词数量</div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="stat-card">
                    <i class="fas fa-user-slash"></i>
                    <div class="stat-number"><?php echo $ban_count; ?></div>
                    <div class="stat-label">封禁IP数量</div>
                </div>
            </div>
        </div>
        
        <h2 class="section-title"><i class="fas fa-heart"></i> 最近的表白</h2>
        <div class="recent-items">
            <?php if (count($recent_confessions) > 0): ?>
                <ul class="item-list">
                    <?php foreach ($recent_confessions as $confession): ?>
                        <li>
                            <div class="item-content">
                                <div class="item-title">
                                    <?php echo $confession['is_anonymous'] ? '匿名用户' : htmlspecialchars($confession['confessor']); ?>
                                    向 <?php echo htmlspecialchars($confession['target_person']); ?> 表白
                                </div>
                                <div class="item-meta">
                                    ID: <?php echo $confession['id']; ?> | 
                                    <?php echo date('Y-m-d H:i', strtotime($confession['created_at'])); ?>
                                </div>
                            </div>
                            <div class="item-actions">
                                <a href="confessions.php?action=edit&id=<?php echo $confession['id']; ?>">编辑</a>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php else: ?>
                <p>暂无表白内容</p>
            <?php endif; ?>
            <a href="confessions.php" class="btn btn-sm btn-primary mt-3">查看全部</a>
        </div>
        
        <h2 class="section-title"><i class="fas fa-coffee"></i> 最近的日常</h2>
        <div class="recent-items">
            <?php if (count($recent_dailies) > 0): ?>
                <ul class="item-list">
                    <?php foreach ($recent_dailies as $daily): ?>
                        <li>
                            <div class="item-content">
                                <div class="item-title">
                                    <?php echo htmlspecialchars($daily['poster']); ?> 的日常
                                </div>
                                <div class="item-meta">
                                    ID: <?php echo $daily['id']; ?> | 
                                    <?php echo date('Y-m-d H:i', strtotime($daily['created_at'])); ?>
                                </div>
                            </div>
                            <div class="item-actions">
                                <a href="dailies.php?action=edit&id=<?php echo $daily['id']; ?>">编辑</a>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php else: ?>
                <p>暂无日常内容</p>
            <?php endif; ?>
            <a href="dailies.php" class="btn btn-sm btn-primary mt-3">查看全部</a>
        </div>
    </main>
</body>
</html>
